home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Crossword / Source / BroadeningSearch.m < prev    next >
Text File  |  1995-06-12  |  1KB  |  101 lines

  1. /*
  2.  
  3. File BroadeningSearch.m
  4.  
  5. This type of search is depth first with iterative broadening.
  6.  
  7. */
  8.  
  9. #import <appkit/appkit.h>
  10.  
  11. #import "BroadeningSearch.h"
  12.  
  13.  
  14. /* ————————————————————————————————————————————————————————————————————————————  */
  15.  
  16.  
  17. #define variable(i)        ([variables  objectAt: i])
  18.  
  19.  
  20. /* ————————————————————————————————————————————————————————————————————————————  */
  21.  
  22.  
  23. @implementation BroadeningSearch
  24.  
  25.  
  26. - free
  27. {
  28.     free(branches);
  29.     return [super  free];
  30. }
  31.  
  32.  
  33. - (BOOL) startSearch: (id) theVariables
  34. {
  35.     branches = (int *) malloc(sizeof(int) * [theVariables  count]);
  36.     bound = 1;
  37.     missedSome = NO;
  38.     
  39.     return [super  startSearch: theVariables];
  40. }
  41.  
  42.  
  43. - (BOOL) step
  44. {
  45.     if (ok)
  46.     {
  47.         [self  findBest];
  48.         branches[next] = 0;
  49.     }
  50.     
  51.     ok = ((branches[next] != bound) && [self  fill: variable(next)]);
  52.     if (!ok)
  53.     {
  54.         if ([self  backup] == NO) return [self  done];
  55.     }
  56.     
  57.     else
  58.     {
  59.         branches[next]++;
  60.         if (++next == count) return [self  done];
  61.     }
  62.     
  63.     return YES;
  64. }
  65.  
  66.  
  67. - (BOOL) backup
  68. {
  69.     if (branches[next] == bound)
  70.     {
  71.         [self  erase: variable(next)];
  72.         missedSome = YES;
  73.     }
  74.     
  75.     if ((next == 0) && !missedSome) return NO;
  76.     else if (next == 0)
  77.     {
  78.         ok = YES;
  79.         bound++;
  80.         missedSome = NO;
  81.         [variables  makeObjectsPerform: @selector(startOver)];
  82.     }
  83.     
  84.     else next--;
  85.     
  86.     return YES;
  87. }
  88.  
  89.  
  90. - erase: (id) variable
  91. {
  92.     [last  unhilight];
  93.     [variable  hilight];
  94.     [last = variable  erase];
  95.     
  96.     return self;
  97. }
  98.  
  99.  
  100. @end
  101.